home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fritz: All Fritz
/
All Fritz.zip
/
All Fritz
/
FILES
/
PROGNG_C
/
DBTOOLC.LZH
/
SOURCE.ARC
/
DBSTAT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1986-10-08
|
18KB
|
771 lines
/*
* NAME:
* stat.c
*
* SYNOPSIS:
* statistics routines for dct1 library
*
* DESCRIPTION:
* 'Low level' statistics functions for dBASE library. All are
* called by a jacketed routine from the main program pak.
*
* RETURNS:
* All functions return the actual result of the calculation as
* a double precision floating point value.
*
* NOTES:
*
* AUTHOR: J. T. Cooper
*
*/
#include <stdio.h>
#include "dctmain.h"
/*
* NAME:
* smin - sample minimum value function
*
* SYNOPSIS:
* double smin(s, n)
* double s[]; -- set (array) of numbers
* int n; -- maximum # of values to check
*
* DESCRIPTION:
* Finds the smallest value in the set
* (s[0],s[1],...,s[n-1]).
*
* RETURNS:
* Returns the minimum of all values in the array as a
* double precision floating point number.
*/
double smin(s, n)
double *s; /* set of double precision floating point numbers */
int n; /* max # of values to check */
{
double smallest;
smallest = s[n-1]; /* set lowest to initial value */
while (n--)
if (s[n] < smallest)
smallest = s[n];
return(smallest);
}
/*
* NAME:
* smax - sample maximum value function
*
* SYNOPSIS:
* double smax(s, n)
* double s[]; -- list (array) of values to check
* int n; -- maximum # of values to check
*
* DESCRIPTION:
* Determines the largest value in the set
* (s[0], s[1], ..., s[n-1])
*
* RETURNS:
* Returns the maximum value as a double precision floating
* point number
*
*/
double smax(s, n)
double *s; /* set of double precision floating point numbers */
int n; /* max # of values to check */
{
double biggest;
biggest = s[n-1]; /* set initial value */
while (n--)
if (s[n] > biggest)
biggest = s[n];
return(biggest);
}
/*
* NAME:
* srange - find the range of samples in a set
*
* SYNOPSIS:
* double srange(s, n)
* double s[]; -- set (array) of double precision #'s
* int n; -- number of elements in set
*
* DESCRIPTION:
* Finds the range of samples in a set; ie, calculates
* smax(s,n) - smin(s,n).
*
* RETURNS:
* Returns the range as a double precision floating point number.
*
*/
double srange(s, n)
double *s; /* pointer to set */
int n; /* size of array */
{
return(smax(s, n) - smin(s, n));
}
/*
* NAME:
* sgtn - find # of samples in a set greater than a value
*
* SYNOPSIS:
* double sgtn(val, s, n)
* double val; -- reference value
* double s[]; -- set (array) of double precision #'s
* int n; -- size of array
*
* DESCRIPTION:
* Returns a count of the number of values in the set s that
* are greater than val.
*
* RETURNS:
* Returns the actual count of values that satisfy the comparison
*
*/
sgtn(val, s, n)
double val; /* value to compare with */
double *s; /* set of numbers (array of doubles) */
int n; /* size of array */
{
int count = 0;
while (n--)
if (s[n] > val)
++ count;
return(count);
}
/*
* NAME:
* sltn - find # of samples in a set less than a value
*
* SYNOPSIS:
* double sltn(val, s, n)
* double val; -- reference value
* double s[]; -- set (array) of double precision #'s
* int n; -- size of array
*
* DESCRIPTION:
* Returns a count of the number of values in the set s that
* are less than val.
*
* RETURNS:
* Returns the actual count of values that satisfy the comparison
*
*/
sltn(val, s, n)
double val; /* value to compare with */
double *s; /* set of numbers (array of doubles) */
int n; /* size of array */
{
int count = 0;
while (n--)
if (s[n] < val)
++ count;
return(count);
}
/*
* NAME:
* seqn - find # of samples in a set equal to a value
*
* SYNOPSIS:
* double seqn(val, s, n)
* double val; -- reference value
* double s[]; -- set (array) of double precision #'s
* int n; -- size of array
*
* DESCRIPTION:
* Returns a count of the number of values in the set s that
* are equal to val.
*
* RETURNS:
* Returns the actual count of values that satisfy the comparison
*
*/
seqn(val, s, n)
double val; /* value to compare with */
double *s; /* set of numbers (array of doubles) */
int n; /* size of array */
{
int count = 0;
while (n--)
if (s[n] == val)
++ count;
return(count);
}
/*
* NAME:
* sgen - find # of samples in a set greater than or
* equal to a given value
*
* SYNOPSIS:
* double sgen(val, s, n)
* double val; -- reference value
* double s[]; -- set (array) of double precision #'s
* int n; -- size of array
*
* DESCRIPTION:
* Returns a count of the number of values in the set s that
* are greater than or equal to val.
*
* RETURNS:
* Returns the actual count of values that satisfy the comparison
*
*/
sgen(val, s, n)
double val; /* value to compare with */
double *s; /* set of numbers (array of doubles) */
int n; /* size of array */
{
int count = 0;
while (n--)
if (s[n] >= val)
++ count;
return(count);
}
/*
* NAME:
* slen - find # of samples in a set less than or equal to
* a given value
*
* SYNOPSIS:
* double slen(val, s, n)
* double val; -- reference value
* double s[]; -- set (array) of double precision #'s
* int n; -- size of array
*
* DESCRIPTION:
* Returns a count of the number of values in the set s that
* are less than or equal to val.
*
* RETURNS:
* Returns the actual count of values that satisfy the comparison
*/
slen(val, s, n)
double val; /* value to compare with */
double *s; /* set of numbers (array of doubles) */
int n; /* size of array */
{
int count = 0;
while (n--)
if (s[n] <= val)
++ count;
return(count);
}
/*
* NAME:
* smean - find mean value of a sample set
*
* SYNOPSIS:
* double smean(s, n)
* double s[]; -- set (array) of numbers
* int n; -- size of array
*
* DESCRIPTION:
* Calculates the mean average of the first n elements of
* the set s, where
*
* mean = (s[0] + s[1] + ... + s[n-1])/n
*
* RETURNS:
* Returns the mean average as a double precision floating
* point number
*
*/
double smean(s, n)
double *s; /* array of numbers */
int n; /* size of array */
{
int i = n;
double stot = 0.0;
while (i--)
stot += s[i];
return(n == 0 ? 0.0 : stot/(double) n);
}
/*
* NAME:
* smedian - median average function
*
* SYNOPSIS:
* double smedian(s,n)
* double s[]; -- set (array) of numbers to average
* int n; -- size of array
*
* DESCRIPTION:
* Calculate the median average of the first n elements of s:
* First sort the elements in ascending order into s1;
* If n is odd
* median = s1[(n+1)/2];
* Otherwise (n is even)
* median = (s1[(n+1)/2] + s1[(n+1)/2 + 1])/2
*
* RETURNS:
* Returns the median value as a double precision float
*
*/
double smedian(s, n)
double *s; /* array of numbers */
int n; /* size of array */
{
double *d; /* place to store sorted array */
double result;
d = (double *)dctalloc(n, sizeof(double));
if (d == NULL)
{
DB_STATUS = -1.0; /* error status */
SetStatus(DB_STATUS);
return(0.0);
}
sros(s, d, n); /* sort array to d */
result = (n % 2 ? d[(n+1)/2 - 1] : (d[n/2 - 1] + d[n/2])/2.0);
free(d);
return (result);
}
/*
* NAME:
* svar - sample variance function
*
* SYNOPSIS:
* double svar(s,n)
* double s[]; -- list (array) of numbers to use
* int n; -- size of array
*
* DESCRIPTION:
* Calculate the variance of the first n samples in the set s:
* First calculate sum of squares of set:
* sqsum = s[0]*s[0] + s[1]*s[1] + . . . s[n-1]*s[n-1]
* var = (sqsum - n*mean*mean) / (n-1)
*
* RETURNS:
* Returns the variance as a double precision float
*
*/
double svar(s, n)
double *s; /* array of numbers */
int n; /* size of array */
{
double sqsum; /* sum of squares */
double mean; /* mean average */
int i;
mean = smean(s, n);
i = n;
/* first calculate sumof(x[i]^2) */
sqsum = 0.0;
while (i--)
sqsum += s[i] * s[i];
return((sqsum - n*mean*mean) / (double) (n-1));
}
/*
* NAME:
* sstdev - standard deviation function
*
* SYNOPSIS:
* double sstdev(s,n)
* double s[]; -- set (array) of numbers
* int n; -- size of array
*
* DESCRIPTION:
* Calculates the standard deviation of the first n
* elements in the set s, by simply calculating the
* square root of the variance of the same set.
*
* RETURNS:
* Returns the standard deviation as a double precision float
*
*/
double sstdev(s, n)
double *s; /* array of numbers */
int n; /* size of array */
{
/* std dev is square root of variance */
return(pow(svar(s, n), .5));
}
/*
* NAME:
* scv - sample coefficient of variance function
*
* SYNOPSIS:
* double scv(s,n)
* double s[]; -- set (array) of numbers
* int n; -- size of array
*
* DESCRIPTION:
* Calculates coefficient of variance of the first n
* elements of the set s:
* cv = standard deviation / mean
*
* RETURNS:
* Returns the coefficient of variance as a double
* precision floating point number
*/
double scv(s, n)
double *s; /* array of doubles */
int n; /* size of array */
{
return(sstdev(s,n) / smean(s,n));
}
/*
* NAME:
* sros - sample reverse order statistics (sample sort) function
*
* SYNOPSIS:
* void sros(src, dest, n)
* double src[]; -- array to sort
* double dest[]; -- place to put sorted array
* int n; -- size of array
*
* DESCRIPTION:
* Sorts the first n elements in src in ascending order,
* placing the sorted array into dest. Uses a simple shell
* sort algorithm
*
* RETURNS:
* Returns n.
*
*/
sros(src, dest, n)
double *src; /* array to sort */
double *dest; /* place to put sorted array */
int n; /* size of array */
{
int gap, i, j;
double temp;
if (src != dest) /* if we're not sorting in place */
{ /* first copy source to dest */
for (i = 0; i < n; i++)
dest[i] = src[i];
}
for (gap = n/2; gap > 0; gap /= 2) /* now sort dest in place */
for (i = gap; i < n; i++)
for (j = i-gap; j >=0; j -= gap)
{
if (dest[j] <= dest[j+gap])
break;
temp = dest[j];
dest[j] = dest[j+gap];
dest[j+gap] = temp;
}
return(n);
}
/*
* NAME:
* sdist - sample frequency distribution function
*
* SYNOPSIS:
* int sdist(s, r, d, f, n)
* double s[]; -- source array
* double r[]; -- place to put sorted array
* double d[]; -- place to put distinct values
* double f[]; -- place to put frequency counts
* int n; -- number of elements in s
*
* DESCRIPTION:
* This function first sorts the first n elements of s
* and places the resulting sorted array in r. Then a list
* of distinct values is placed, from lowest to highest,
* in d; ie, d is then a list which contains exactly one
* occurrence of all values which occur at least once in
* s. A count of each unique value is placed in f, so that
* f[0] = count of the number of times d[0] occurs in s.
*
* RETURNS:
* Returns the number of distinct values; ie, the size of d.
*
*/
sdist(s, r, d, f, n)
double *s, /* source array */
*r, /* will hold sorted array */
*d, /* place to put distinct values */
*f; /* place to put frequency counts */
int n; /* size of original array (s) */
{
int k; /* actual number of values in d */
int j; /* index to unique values */
sros(s, r, n); /* product a sorted array */
for (j = k = 0; j < n; k++)
{
d[k] = r[j]; /* copy first occurrence */
f[k] = seqn(d[k], r, n); /* count # of occurrences */
j += f[k]; /* skip past repeated numbers */
}
return(k); /* return number of distinct values */
}
/*
* NAME:
* scovar - sample covariance function
*
* SYNOPSIS:
* double scovar(x, y, n)
* double x[]; array of first members of pairs
* double y[]; array of second member of pairs/
* int n; size of array
*
* DESCRIPTION:
* Calculates the covariance of a list of pairs, where
* x contains the first members of the list, and y contains
* the second members.
*
* RETURNS:
* Returns the covariance as a double precision float.
*/
double scovar(x, y, n)
double *x; /* array of first members of pairs */
double *y; /* array of second members of pairs */
int n; /* size of array */
{
double pairsum;
double xmean;
double ymean;
int i;
xmean = smean(x, n);
ymean = smean(y, n);
i = n;
pairsum = 0.0;
while (i--)
pairsum += (x[i] * y[i]);
return((pairsum - n * xmean * ymean) / (double) (n - 1));
}
/*
* NAME:
* scorr - sample correlation function
*
* SYNOPSIS:
* double scorr(x, y, n)
* double x[]; -- array of first members of pairs
* double y[]; -- array of second members of pairs
* int n; -- size of array
*
* DESCRIPTION:
* Calculates the sample correlation of a list of pairs,
* where x contains the first members of the list, and y
* contains the second members.
*
* RETURNS:
* Returns the correlation as a double precision float.
*
*/
double scorr(x, y, n)
double *x; /* array of first members of pairs */
double *y; /* array of second members of pairs */
int n; /* size of array */
{
return(scovar(x,y,n) / (sstdev(x,n) * sstdev(y,n)));
}
/*
* NAME:
* snsk - sample normal scores function
*
* SYNOPSIS:
* void snsk(x, z, n)
* double x[]; -- initial array to use
* double z[]; -- place to put results
* int n; -- size of array
*
* DESCRIPTION:
* Places in z the 'normal scores' of x, where
* z[i] = (x[i] - mean(x)) / standard deviation of x
*
* RETURNS:
* Returns nothing, but fills z with calculated values.
*
*/
void snsk(x, z, n)
double *x, /* initial array */
*z; /* place to put results */
int n; /* size of array */
{
double xmean;
int i;
xmean = smean(x, n);
i = n;
while (i--)
z[i] = (x[i] - xmean) / sstdev(x, n);
}
/*
* NAME:
* sskew - sample skewness function
*
* SYNOPSIS:
* double sskew(z, n)
* double z[]; -- array presumably created by snsk()
* int n; -- size of array
*
* DESCRIPTION:
* Calculates the sample skewness of an array, where z is a list
* of normal scores produced from that array. Skewness is a
* measure of assymmetry, and is calculated as follows:
*
* skew = 1/n * (z[0]^3 + z[1]^3 + ... + z[n-1]^3)
*
* RETURNS:
* Returns the sample skewness as a double precision float.
*/
double sskew(z, n)
double *z; /* array to use */
int n; /* size of array */
{
double ssum = 0.0;
int i;
i = n;
while (i--)
ssum += (z[i] * z[i] * z[i]);
return(ssum/(double) n);
}
/*
* NAME:
* skurt - sample kurtosis function
*
* SYNOPSIS:
* double skurt(z, n)
* double z[]; -- array to use, presumably created by nsk()
* int n; -- size of array
*
* DESCRIPTION:
* Calculates the sample kurtosis of an array, where z is a
* list of normal scores produced from that array. Kurtosis is
* a measure of 'peakedness', and is 0 for perfectly normal
* distributions. It is calculated as follows:
*
* kurt = 1/n * (z[0]^4 + z[1]^4 + ... + z[n-1]^4) - 3
*
* RETURNS:
* Returns the sample kurtosis as a double precision float.
*
*/
double skurt(z, n)
double *z; /* array to use */
int n; /* size of array */
{
double ksum = 0;
int i;
i = n;
while (i--)
ksum += (z[i] * z[i] * z[i] * z[i]);
return(ksum/(double)n - 3.0);
}
/*
* NAME:
* chisq - chi square function
*
* SYNOPSIS:
* double chisq(exp, obs, n)
* double exp; -- array of expected values
* double obs; -- array of observed values
* int n; -- size of arrays
*
* DESCRIPTION:
* Calculates the chi-square statistic, according to
* the following formula:
*
* V = (exp[0]-obs[0])^2 / exp[0]
* + (exp[1]-obs[1])^2 / exp[1]
* ...
* + (exp[n-1]-obs[n-1])^2 / exp[n-1]
*
* RETURNS:
* Returns the chi-square statistic double precision float.
*
*/
double chisq(exp,obs,n)
double *exp; /* array of 'expected' values */
double *obs; /* array of observed values */
int n; /* size of array */
{
double V = 0.0;
while (n--)
#ifdef AZTEC
V += pow(fabs(exp[n] - obs[n]), 2.0) / exp[n];
#endif
#ifdef MS
V += pow(fabs(exp[n] - obs[n]), 2.0) / exp[n];
#endif
#ifdef LATTICE
V += pow(exp[n] - obs[n], 2.0) / exp[n];
#endif
return(V);
}